package com.zff.rental.file;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.spi.FileSystemProvider;

/**
 * 写入文件
 * 写入文件可以通过 newOutputStream 或 newFileChannel 方法获取输出流或文件通道，然后写入数据。
 */
public class WriteFileExample {
    public static void main(String[] args) {
        FileSystemProvider provider = FileSystems.getDefault().provider();
        Path filePath = Paths.get("example.txt");

        try {
            // 使用 newOutputStream 写入文件
            try (var outputStream = provider.newOutputStream(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
                String content = "Hello, FileSystemProvider!";
                outputStream.write(content.getBytes());
            }

            System.out.println("文件写入成功: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}